package the.broccoli.uc.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.wisteria.framework.web.QueryResult;
import org.wisteria.framework.web.mvc.service.AbstractSuperService;
import the.broccoli.uc.mapper.UserMapper;
import the.broccoli.uc.entity.User;
import the.broccoli.uc.service.IUserService;
import the.broccoli.uc.vo.UserVO;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author lei.liu
 * @since 2018-10-12
 */
@Service
public class UserServiceImpl extends AbstractSuperService<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 新增用户
     *
     * @param account      账号
     * @param userName     用户名
     * @param phone        手机号码
     * @param email        邮箱
     * @param operatorId   操作人ID
     * @param operatorName 操作人名称
     * @param tenantId     租户ID
     * @return
     */
    @Override
    public boolean save(String account, String userName, String phone, String email, String operatorId, String operatorName, String tenantId) {
        User entity = new User();
        entity.setAccount(account);
        entity.setUserName(userName);
        entity.setPhone(phone);
        entity.setEmail(email);
        entity.setPassword("");
        entity.setIsDeleted(0);
        entity.setCreatedId(operatorId);
        entity.setCreatedName(operatorName);
        entity.setCreatedTime(new Date());
        entity.setTenantId(tenantId);
        return save(entity);
    }

    /**
     * 通过登录账号，手机号码，邮箱地址。获取用户信息
     *
     * @param loginAccount 登录账号、手机号码、邮箱地址
     * @return 用户信息
     */
    @Override
    public User getByAccount(String loginAccount) {
        return userMapper.selectByAccount(loginAccount);
    }

    /**
     * 分页查询
     *
     * @param vo        查询条件
     * @param pageNum   页码
     * @param pageSize  每页记录大小
     * @return 查询结果列表
     */
    @Override
    public QueryResult<UserVO> selectPageVO(UserVO vo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new QueryResult(userMapper.selectPageVO(vo));
    }

    /**
     * 修改用户
     *
     * @param userId       用户ID
     * @param account      账号
     * @param userName     用户名
     * @param phone        手机号码
     * @param email        邮箱
     * @param operatorId   操作人ID
     * @param operatorName 操作人名称
     * @return
     */
    @Override
    public boolean update(String userId, String account, String userName, String phone, String email, String operatorId, String operatorName) {
        User entity = new User();
        entity.setUserId(userId);
        entity.setAccount(account);
        entity.setUserName(userName);
        entity.setPhone(phone);
        entity.setEmail(email);
        entity.setModifiedId(operatorId);
        entity.setModifiedName(operatorName);
        entity.setModifiedTime(new Date());
        return updateById(entity);
    }

}
